package cn.com.cetccst.appstore.tools;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import cn.com.cetccst.appstore.R;

public class CommonAlert {
    public static void showAlertDialog(Context context, String title, String message, final View.OnClickListener agreeListener
            , boolean onSure, String agreeText, String cancelText) {
        if (context instanceof Activity) {
            Activity activity = ((Activity) context);
            if (activity.isFinishing() || activity.isDestroyed()) {
                return;
            }
        }
        View view = LayoutInflater.from(context).inflate(R.layout.layout_conmon_alert_dialog, null, false);
        final AlertDialog dialog = new AlertDialog.Builder(context).setView(view).create();
        TextView tvTitle = view.findViewById(R.id.title);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        TextView content = view.findViewById(R.id.content);
        content.setText(message);
        TextView cancelHighOption = view.findViewById(R.id.cancel);
        if (!TextUtils.isEmpty(cancelText)) {
            cancelHighOption.setText(cancelText);
        }
        TextView agreeHighOption = view.findViewById(R.id.confirm);
        if (!TextUtils.isEmpty(agreeText)) {
            agreeHighOption.setText(agreeText);
        }
        cancelHighOption.setOnClickListener(v -> dialog.dismiss());
        if (onSure || agreeListener == null) {
            cancelHighOption.setVisibility(View.GONE);
        }
        agreeHighOption.setOnClickListener(v -> {
            if (agreeListener != null) {
                agreeListener.onClick(v);
            }
            dialog.dismiss();
        });
        if (onSure) {
            dialog.setCancelable(false);
        }
        dialog.show();
    }

    public static void showAlertDialog(Context context, String title, String message, final View.OnClickListener agreeListener, boolean onSure) {
        showAlertDialog(context, title, message, agreeListener, onSure, "", "");
    }
    public static void showAlertDialog(Context context, String message, final View.OnClickListener agreeListener, boolean onSure) {
        showAlertDialog(context, null, message, agreeListener, onSure);
    }

    public static void showAlertDialog(Context context, String message, final View.OnClickListener agreeListener) {
        showAlertDialog(context, message, agreeListener, false);
    }
}
